package org.dllwh.nacos.sdk;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.setting.dialect.Props;
import cn.hutool.setting.dialect.PropsUtil;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingMaintainFactory;
import com.alibaba.nacos.api.naming.NamingMaintainService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.Service;
import com.alibaba.nacos.api.utils.StringUtils;
import com.alibaba.nacos.common.utils.MapUtil;
import com.alibaba.nacos.shaded.com.google.common.collect.Maps;

import java.util.Map;

/**
 * 把今天最好的表现当作明天最新的起点．．～
 * <p>
 * Today the best performance as tomorrow the newest starter!
 *
 * @ClassName NacosNamingMaintainServiceTemplate
 * @Description Nacos 元数据维护服务
 * @Date 2025-08-27 9:36
 * @版本 V 1.0.1
 * @since JDK 17
 */
public class NacosMaintainServiceTemplate {
    private static final Props PROPS = PropsUtil.get("nacos.properties");

    /**
     * 创建Nacos服务
     *
     * @param serviceName 服务名称。
     * @param groupName   分组名称。
     */
    public void createNacosService(String serviceName, String groupName) throws NacosException {
        NamingMaintainService namingMaintainService = NamingMaintainFactory.createMaintainService(PROPS);
        if (StringUtils.isBlank(groupName)) {
            namingMaintainService.createService(serviceName);
        } else {
            namingMaintainService.createService(serviceName, groupName);
        }
    }

    /**
     * 删除Nacos服务
     *
     * @param serviceName 服务名称。
     * @param groupName   分组名称。
     */
    public boolean deleteNacosService(String serviceName, String groupName) throws NacosException {
        NamingMaintainService namingMaintainService = NamingMaintainFactory.createMaintainService(PROPS);
        if (StringUtils.isBlank(groupName)) {
            return namingMaintainService.deleteService(serviceName);
        } else {
            return namingMaintainService.deleteService(serviceName, groupName);
        }
    }

    /**
     * 更新Nacos服务
     *
     * @param serviceName      服务名称。
     * @param groupName        分组名称。
     * @param protectThreshold 保护阈值
     * @param metadata         元数据
     */
    public void updateNacosService(String serviceName, String groupName, float protectThreshold, Map<String, String> metadata) throws NacosException {
        NamingMaintainService namingMaintainService = NamingMaintainFactory.createMaintainService(PROPS);
        if (MapUtil.isNotEmpty(metadata)) {
            namingMaintainService.updateService(serviceName, groupName, protectThreshold, metadata);
        } else {
            namingMaintainService.updateService(serviceName, groupName, protectThreshold);
        }
    }

    /**
     * 更新Nacos实例信息
     *
     * @param serviceName 服务名称。
     * @param groupName   分组名称。
     * @param instance    实例元数据
     */
    public void updateNacosInstance(String serviceName, String groupName, Instance instance) throws NacosException {
        NamingMaintainService namingMaintainService = NamingMaintainFactory.createMaintainService(PROPS);
        if (StringUtils.isBlank(groupName)) {
            namingMaintainService.updateInstance(serviceName, instance);
        } else {
            namingMaintainService.updateInstance(serviceName, groupName, instance);
        }
    }
}
